
package com.lmx.persistence.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.lmx.common.aliyun.signature.Signature;
import com.lmx.common.aliyun.signature.The;
import com.lmx.common.util.HttpRequestUtil;

import net.sf.json.JSONObject;

/**
 * 
 * 
 * @author huibati
 * @version 1.00
 */
@Service
@Scope("prototype")
public class ALiService<T> {

	private Log logger = LogFactory.getLog(this.getClass());

	/**
	 * 暂停直播
	 * 
	 * @return
	 */
	public String forbidLiveStream(String domainName, String appName, String streamName) {
		String ACTION = "ForbidLiveStream";
		String API_URL = The.API.build(ACTION);
		API_URL += "&DomainName=" + domainName + "&AppName=" + appName + "&StreamName=" + streamName
				+ "&LiveStreamType=publisher";
		String requestUrl = Signature.newBuilder().method(The.HTTP.GET.method()).url(API_URL).secret(The.API.secret())
				.build().compose();
		// HttpGet pmethod = new HttpGet(requestUrl);
		// // 设置响应头信息
		// pmethod.addHeader("Connection", "keep-alive");
		// pmethod.addHeader("Cache-Control", "max-age=0");
		// pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0;
		// Windows NT 6.0) ");
		// pmethod.addHeader("Accept",
		// "text/html,application/xhtml+xml,application/xml;q=0.9,*/;q=0.8");
		// DefaultHttpClient client = new DefaultHttpClient();
		// try {
		// HttpResponse response =client.execute(pmethod);
		// String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
		// return jsonStr;
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// return "";
		// }
		String res = HttpRequestUtil.sendGet(requestUrl);
		logger.debug("ForbidLiveStream live res :" + res);
		JSONObject resJson = JSONObject.fromObject(res);
		if (resJson.containsKey("RequestId")) {
			return "200";
		} else {
			return "error";
		}
	}

	/**
	 * 恢复直播
	 * 
	 * @return
	 */
	public String resumeLiveStream(String domainName, String appName, String streamName) {
		String ACTION = "ResumeLiveStream";
		String API_URL = The.API.build(ACTION);
		API_URL += "&DomainName=" + domainName + "&AppName=" + appName + "&StreamName=" + streamName
				+ "&LiveStreamType=publisher";
		String requestUrl = Signature.newBuilder().method(The.HTTP.GET.method()).url(API_URL).secret(The.API.secret())
				.build().compose();
		String res = HttpRequestUtil.sendGet(requestUrl);
		logger.debug("ResumeLiveStream live res :" + res);
		JSONObject resJson = JSONObject.fromObject(res);
		if (resJson.containsKey("RequestId")) {
			return "200";
		} else {
			return "error";
		}
	}

	/**
	 * 查询推流在线列表
	 * 
	 * @return
	 */
	public String describeLiveStreamsOnlineList(String domainName, String appName, String streamName) {
		String ACTION = "DescribeLiveStreamsOnlineList";
		String API_URL = The.API.build(ACTION);
		API_URL += "&DomainName=" + domainName + "&AppName=" + appName + "&StreamName=" + streamName;
		String requestUrl = Signature.newBuilder().method(The.HTTP.GET.method()).url(API_URL).secret(The.API.secret())
				.build().compose();
		String res = HttpRequestUtil.httpsGet(requestUrl);
		logger.debug("DescribeLiveStreamsOnlineList live res :" + res);
		JSONObject resJson = JSONObject.fromObject(res);
		return resJson.toString();
	}

	/**
	 * 增添直播录制
	 * 
	 * @return
	 */
	public String addLiveAppRecordConfig(String domainName, String appName, String streamName) {
		String ACTION = "AddLiveAppRecordConfig";
		String API_URL = The.API.build(ACTION);
		API_URL += "&DomainName=" + domainName + "&AppName=" + appName + "&OssEndpoint=oss-cn-hangzhou.aliyuncs.com"
				+ "&OssBucket=" + ""// 需要在控制平台建立存储bucket
				+ "&RecordFormat.1.Name=flv"
				+ "&RecordFormat.1.OssObjectPrefix=record/{AppName}/{StreamName}/{Sequence}"
				+ "&RecordFormat.2.Name=mp4"
				+ "&RecordFormat.2.OssObjectPrefix=record/{AppName}/{StreamName}/{UnixTimestamp}_{Sequence}";
		String requestUrl = Signature.newBuilder().method(The.HTTP.GET.method()).url(API_URL).secret(The.API.secret())
				.build().compose();
		String res = HttpRequestUtil.httpsGet(requestUrl);
		logger.debug("AddLiveAppRecordConfig live res :" + res);
		JSONObject resJson = JSONObject.fromObject(res);
		return resJson.toString();
	}

}
